package com.sx.pipeline.utils;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import androidx.core.app.ActivityCompat;

import com.amap.api.maps.model.LatLng;
import com.sx.mvvmlibs.utils.Logs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * @author: YY
 * @time: 2021/5/27
 * @describe Android 原生定位 工具类
 */
public class LocationUtils {

    private static final double EARTH_RADIUS = 6378137.0;

    // 纬度
    public static double latitude = 0.0;
    // 经度
    public static double longitude = 0.0;

    public static Location location;

    //初始化位置信息
    public static void initLocation(Context context) {
        LocationManager locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                return;
            }
             location = locationManager
                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
            if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
            }
        } else {
            LocationListener locationListener = new LocationListener() {

                // Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
                @Override
                public void onStatusChanged(String provider, int status,
                                            Bundle extras) {

                }

                // Provider被enable时触发此函数，比如GPS被打开
                @Override
                public void onProviderEnabled(String provider) {

                }

                // Provider被disable时触发此函数，比如GPS被关闭
                @Override
                public void onProviderDisabled(String provider) {

                }

                // 当坐标改变时触发此函数，如果Provider传进相同的坐标，它就不会被触发
                @Override
                public void onLocationChanged(Location location) {
                    if (location != null) {

                    }
                }
            };
            locationManager
                    .requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                            1000, 0, locationListener);
             location = locationManager
                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null) {
                // 经度
                latitude = location.getLatitude();
                // 纬度
                longitude = location.getLongitude();
            }
        }
    }

    // 返回单位是米
    public static double getDistance(double longitude1, double latitude1,
                                     double longitude2, double latitude2) {
        double Lat1 = rad(latitude1);
        double Lat2 = rad(latitude2);
        double a = Lat1 - Lat2;
        double b = rad(longitude1) - rad(longitude2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(Lat1) * Math.cos(Lat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        return s;
    }

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    //获取地址信息:城市、街道等信息
    public static Address getAddress(Context context) {
        List<Address> result;
        try {
            if (location != null) {
                Geocoder gc = new Geocoder(context, Locale.getDefault());
                result = gc.getFromLocation(location.getLatitude(),
                        location.getLongitude(), 1);
                Logs.d("获取地址信息："+result.get(0).getSubAdminArea()+"====Latitude===:"+result.get(0).getLatitude()+"====Longitude===:"+result.get(0).getLongitude());
                return result.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Address(new Locale(""));
    }


    //根据名称获取经纬度
    public static LatLng getLatAndLng(String cityName, Context context) {

        Geocoder geocoder = new Geocoder(context,Locale.CHINA);
        try {
            List<Address> addressList = geocoder.getFromLocationName(cityName,5);
            if (addressList.size()> 0) {
                Address address = addressList.get(0);
                double latitude = address.getLatitude();//纬度
                double longitude = address.getLongitude();//精度
                String mSelectProvince = address.getAdminArea();// 省份名称
                String mSelectArea = address.getSubAdminArea();//城市名称
                LatLng locationLatLng = new LatLng(latitude,longitude);
                Logs.d("====Latitude===:"+mSelectProvince+mSelectArea+locationLatLng.latitude+"====Longitude===:"+locationLatLng.longitude);
                return locationLatLng;
            }
        }catch (IOException e) {
            e.printStackTrace();
        }
        return new LatLng(0,0);
    }
}
